package sfeir.ergosum.adapter;

import java.util.ArrayList;

import sfeir.ergosum.R;
import sfeir.ergosum.model.Email;
import sfeir.ergosum.model.Person;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MailAdapter extends BaseAdapter {

	private Context mContext;
	private LayoutInflater mInflater;
	private ArrayList<Email> details;
	private Person p;
	private ViewHolder holder;

	public MailAdapter(Context c, Person p) {
		mContext = c;
		this.p = p;
		mInflater = LayoutInflater.from(mContext);
		details = new ArrayList<Email>();
		populate();
	}

	public void update(Person newP) {
		p = newP;
		details.clear();
		populate();
		notifyDataSetChanged();
	}

	private void populate() {
		for (Email t : p.getEmails())
			details.add(t);
	}

	private static class ViewHolder {
		ImageView logo;
		TextView info;
	}

	@Override
	public int getCount() {
		return details.size();
	}

	@Override
	public Email getItem(int position) {
		return details.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.list_item, null);

			holder = new ViewHolder();
			holder.logo = (ImageView) convertView.findViewById(R.id.list_item_image);
			holder.info = (TextView) convertView.findViewById(R.id.list_item_content);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		Email t = getItem(position);
		

		final String email = t.getEmail();
		final String[] mailto = { email };
		OnClickListener emailListener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent sendIntent = new Intent(Intent.ACTION_SEND);
				sendIntent.setData(Uri.parse("mailto:" + mailto));
				sendIntent.putExtra(Intent.EXTRA_EMAIL, mailto);
				sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Sujet");
				sendIntent.setType("text/plain");
				mContext.startActivity(Intent.createChooser(sendIntent, "Choix du client mail"));
			}
		};

		holder.info.setText(email);
		convertView.setClickable(true);
		convertView.setOnClickListener(emailListener);

		switch (t.getTypeEmail()) {

		case Email.PERSONAL_EMAIL:
			holder.logo.setImageResource(R.drawable.email_perso);
			break;
		case Email.PROFESSIONNAL_EMAIL:
			holder.logo.setImageResource(R.drawable.email_pro);
			break;
		case Email.UNKNOWN:
			holder.logo.setImageResource(R.drawable.email);
			break;
		default:
			holder.logo.setImageResource(R.drawable.email);
			break;
		}
		
		return convertView;
	}
}
